//
//  VisiableTableController.swift
//  weibobbaj
//
//  Created by Tiffany宝 on 16/4/17.
//  Copyright © 2016年 Tiffany. All rights reserved.
//

import UIKit

class VisiableTableController: UITableViewController,VisiableViewDelegate {
    
    //用户是否登录
    let userLogin: Bool = UserAccountViewModel.sharedViewModel.isLogin
    
    // 可以在loadView里面判断是否登录，没有登录初始化访客视图并设置到 view 上
    override func loadView() {
        
        //        if userLogin {
        //
        //             super.loadView()
        //
        //        }else{
        //
        //            let v = VisiableView()
        //
        //            self.view = v
        //        }
        
        //简化
        userLogin ? super.loadView() : Visiable()
    }
    
    //懒加载控件
    // MARK: - 注意:此对象不能私有化,不然外界无法访问
    lazy var vbView: VisiableView = {
        
        let vb = VisiableView()
        
       //设置代理
        vb.delegate = self
        
        return vb
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
    }
    
    private func Visiable(){
        
        self.view = vbView
        
        //        vbView.registerClosure = {
        //
        //            self.didClickRegisterButton()
        //        }
        
        //设置导航栏左右的按钮
        navigationItem.leftBarButtonItem = UIBarButtonItem(title: "注册", style: .Plain, target: self, action: "didClickRegisterButton")
        navigationItem.rightBarButtonItem = UIBarButtonItem(title: "登录", style: .Plain, target: self, action: "didClickLoginButton")
        
    }
    
    func didClickRegisterButton(){
        
        print("didClickRegisterButton")
    }
    
    // MARK: - 点击登录按钮
    func didClickLoginButton(){
        
        print("didClickLoginButton")
        
        //modal 一个控制器
        let nav = UINavigationController(rootViewController: OAuthViewController())
        
        presentViewController(nav, animated: true, completion: nil)
    }
    
    
}
